##### WTP analysis washing machines  ######

rm(list = ls())


# Load required packages
library(cregg)
library(ggplot2)
library(dfidx)
library(mlogit)
library(dplyr)
library(readr)
library(stargazer)


# Load conjoint data
dfconj <- read_csv("/Users/cbrugge/Desktop/CE Paper/wtp_data_ce.csv") %>% 
  as.data.frame() %>% 
  select(-"...1")


# Prep data for dfidx  ----------------------------------------------------

dfconj_prod1 <- subset(dfconj, product == 1)

colnames(dfconj_prod1) <- c("ID", "product" , "roundid", "choice", "cost.A", "life.A", "env.A", 
                           "eff.A", "recy.A", "rep.A", "product.type")

dfconj_prod1 <- subset(dfconj_prod1, select = -c(product))

dfconj_prod2 <- subset(dfconj, product == 2)

colnames(dfconj_prod2) <- c("ID", "product" , "roundid", "choice", "cost.B", "life.B", "env.B", 
                           "eff.B", "recy.B", "rep.B", "product.type")


dfconj_prod2 <- subset(dfconj_prod2, select = -c(product, choice, roundid, ID, product.type))

data <- cbind(dfconj_prod1, dfconj_prod2)

col_order <- c("ID", "roundid", "choice", "cost.A", "life.A", "env.A", "eff.A", "recy.A", "rep.A", 
               "cost.B", "life.B", "env.B", "eff.B", "recy.B", "rep.B", "product.type")

data <- data[, col_order]

# convert relevant variables to characters
data <- data %>%
  mutate(ID = as.character(ID),
         roundid = as.character(roundid),
         choice = as.character(choice))

#add character string to ID 
data$ID <- paste("R_", data$ID, sep = "")

data <- data %>% mutate(choice = ifelse(choice == "1", "A", "B"))

# Create a new unique identifier by combining "round_id" and "ID"
data$unique_id <- as.character(interaction(data$roundid, data$ID, sep = "."))


# create individual datasets by product -----------------------------------
data_smartphone <- subset(data, product.type == "Smartphone")
data_tv<- subset(data, product.type == "TV")
data_washingmachine<- subset(data, product.type == "Washing machine")

# Convert them to the new values 250, 850, 1900, and 3100 to allow non-linear analysis
data_washingmachine <- data_washingmachine %>%
  mutate(cost.A = case_when(
    cost.A == 1 ~ 470,
    cost.A == 2 ~ 890,
    cost.A == 3 ~ 1650 ,
    cost.A == 4 ~ 3200,
    TRUE ~ cost.A  # Keep unchanged if not matching any condition
  ),
  cost.B = case_when(
    cost.B == 1 ~ 470,
    cost.B == 2 ~ 890,
    cost.B == 3 ~ 1650,
    cost.B == 4 ~ 3200,
    TRUE ~ cost.B  # Keep unchanged if not matching any condition
  ))

# apply dfidx --------------------------------------------------------------

#mlogdata <- dfidx(data, idx = "unique_id", choice = "choice", varying = 4:15, sep = ".")
mlogdata <- dfidx(data_washingmachine, idx = "unique_id", choice = "choice", varying = 4:15, sep = ".")

# calculate mlogit and wtp ------------------------------------------------
m1_wm <- mlogit(choice ~ cost + life + env + eff + recy + rep | -1,
             mlogdata)
summary(m1_wm)

stargazer(m1_wm, type = "text", covariate.labels = c("Price","Function duration","Environmental impact during production",
                                                  "Energy efficiency","Recyclability","Repairability"), dep.var.labels =
            "Product choice")


stargazer(m1_wm, type = "html", covariate.labels = c("Price","Function duration","Environmental impact during production",
                                                     "Energy efficiency","Recyclability","Repairability"), dep.var.labels =
            "Product choice (washing machine)", out = "/Users/cbrugge/Desktop/CE Paper/mixed_logit_wm.html")


wtp <- coef(m1_wm)[2:length(coef(m1_wm))] / coef(m1_wm)["cost"]
wtp
#After running this code, the wtp variable will contain the calculated WTP values
#for each attribute level (excluding the intercept) relative to a one-point increase
#in the price attribute.

# Transform the WTP values to percentage change
wtp_percentage_change <- (exp(wtp) - 1) * 100
print(wtp_percentage_change)



